package cate.game.pvp.pointrace.data;

import cate.game.framework.common.compress.CompressedContent;
import cate.game.framework.common.compress.PlaySideCompressHelper;
import cate.game.play.part.PlaySide;
import easy.java.practice.errorcode.EcResult;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.IOException;

import static cate.game.pvp.pointrace.data.PointRaceOpponent.ofCopy;

@NoArgsConstructor
@Data
@Document(collection = "point-race-ps")
public class PointRacePs {

    public static final String INDEX_NAME = "INDEX";
    public static final String INDEX_PLAYER = "INDEX_PLAYER";
    public static final String INDEX_ROBOT = "INDEX_ROBOT";


    public static PointRacePs compress(PlaySide ps, long points) throws IOException {
        PointRacePs pps = new PointRacePs();
        pps.setUid(ps.snapshot.uid);
        pps.setOpponent(ofCopy(ps.snapshot, points));
        pps.setCps(PlaySideCompressHelper.compress(ps));
        return pps;
    }

    public static EcResult<PointRacePs> compressEc(PlaySide playSide, long points) {
        EcResult<PointRacePs> r = new EcResult<>();
        try {
            r.data = compress(playSide, points);
        } catch (Throwable cause) {
            return r.fail(cause);
        }
        return r.success();
    }

    @Id
    private String uid;

    private PointRaceOpponent opponent;

    private CompressedContent cps;
    public String getIndexName() {
        return getUid();
    }

    public String getIndexRobotName() {
        return String.valueOf(opponent.getRid());
    }

}
